<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * get all projects
 *
   USAGE:
   <pre>

   $ProjectGetAll = $this->CoreModel('ProjectGetAll');

   $params  = array('has_controller' => bool,   // if true, a project must have an assigned public controller
                    'id_status'      => array); // optional , ex: array('>=', 200) // project which are at least active

   $result  = $ProjectGetAll->run( $params );

   if ($result instanceof \Core\Library\Exception) {
       return $this->error( $result->getMessage(), __file__, __line__);
   } else {
       $this->view->result = $result;
   }
   </pre>
 * @package GeoContexter
 * @subpackage Module_Geocontexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (So, 27 Feb 2011) $ / $LastChangedBy: armand.turpel $
 */

namespace Gospecies\Model;

use Core\Model\AbstractModel;
use Core\Model\InterfaceModel;

class ProjectGetFromUser extends    AbstractModel
                         implements InterfaceModel
{
    /**
     * get all projects
     *
     *
     * @param array $params
     */
    public function run( $params )
    {
        try {

            $this->validate_params($params);

            // if the system_serial check must be included
            //
            $_system_serial = "";
            if (isset($params['system_serial']) && $params['system_serial'] == true) {
                $_system_serial = ", geocontexter.gc_system_is_serial(id_project) AS system_serial";
            }

            // get projects which are related to user and to all registered users
            //
            $sql = 'SELECT  gp.* '.$_system_serial.'
                    FROM  geocontexter.gc_project AS gp
                    INNER JOIN geocontexter.gc_project_user AS gpu
                    ON gpu.id_project = gp.id_project
                    WHERE gpu.id_user = ' . $params['id_user'] . '
                    AND gp.id_status = 400

                    UNION

                    SELECT  * '.$_system_serial.'
                    FROM  geocontexter.gc_project
                    WHERE id_status = 300
                    ORDER BY title
                    ';

            return $this->query($sql);

        } catch(\Exception $e) {
            throw $e;
        }
    }

    /**
     * set and validate parameters
     *
     *
     * @param array $params
     */
    private function validate_params( & $params )
    {
        if (isset($params['id_user'])) {

          $val_digits = new \Zend\Validator\Digits();

          if (false === $val_digits->isValid($params['id_user'])) {
              throw new \Exception('id_user isnt from type bigint');
          }

        } else {

          throw new \Exception('id_user isnt defined');

        }

        if (isset($params['id_status'])) {

            if (!isset($params['id_status'][0])) {
                throw new \Exception('id_status array index 0 value not set');
            }
            if (!isset($params['id_status'][1]))  {
                throw new \Exception('id_status array index 1 value not set');
            }

            if(!in_array($params['id_status'][0], array(">","<",">=","<=","=","!="))) {
                throw new \Exception('id_status array index 0 value is wrong: ' . var_export($params['id_status'][0],true));
            }

            if (!in_array($params['id_status'][1], array(0,100,200,300))) {
                throw new \Exception('id_status array index 1 value is wrong: ' . var_export($params['id_status'][1],true));
            }
        }
    }
}
